package com.igoal.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.ComponentEnum;
import com.igoal.entity.DataStatus;
import com.igoal.entity.User;
import com.igoal.entity.UserThumb;

@Repository("userThumbDAO")
public class UserThumbDAOImpl extends GenericDaoHibernate<UserThumb, Long>
		implements UserThumbDAO {

	public UserThumbDAOImpl() {
		super(UserThumb.class);
	}

	public List<UserThumb> listThumb(ComponentEnum component, Long componentId,
			boolean up) {
		DetachedCriteria dc = DetachedCriteria.forClass(UserThumb.class);
		dc.add(Restrictions.eq("component", component));
		dc.add(Restrictions.eq("componentId", componentId));
		dc.add(Restrictions.eq("up", up));
		getByStatus(dc, DataStatus.USING);
		
		return findByCriteria(dc);
	}

	public int countThumb(ComponentEnum component, Long componentId, boolean up) {
		DetachedCriteria dc = DetachedCriteria.forClass(UserThumb.class);
		dc.add(Restrictions.eq("component", component));
		dc.add(Restrictions.eq("componentId", componentId));
		dc.add(Restrictions.eq("up", up));
		getByStatus(dc, DataStatus.USING);
		
		return countByCriteria(dc);
	}
	
	public UserThumb findByUserAndComponent(User user, ComponentEnum component, Long componentId){
		DetachedCriteria dc = DetachedCriteria.forClass(UserThumb.class);
		dc.add(Restrictions.eq("user", user));
		dc.add(Restrictions.eq("component", component));
		dc.add(Restrictions.eq("componentId", componentId));
		getByStatus(dc, DataStatus.USING);
		
		List<UserThumb> result = findByCriteria(dc, 0, 1);
		
		if(result.size() > 0){
			return result.get(0);
		}
		
		return null;
	}
}
